
package com.huizhan.web.controller;
 
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.*;
import javax.persistence.*;
import java.util.*;

import com.huizhan.web.dao.*;
import com.huizhan.web.model.*;

/**
 * 表单数据api
 */
 
@Api(tags = "FormData",description = "表单数据")
@RestController
public class FormDataController {
    
    @Autowired
    private FormDataDao dao;
   
    /**
     * 获取表单数据集合
     * @return api结果
     */
    @ApiOperation(value = "获取表单数据集合")
    @RequestMapping(value =  "/api/FormData/",method = {RequestMethod.GET})
    public ApiResult<List<FormData>> Get(){
         ApiResult<List<FormData>> result=new ApiResult<List<FormData>>();
        try 
        {
            List<FormData> res = dao.findAll();
            result.setData(res);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据ID获取表单数据信息
     * @param id 表单数据ID
     * @return api结果
     */
    @ApiOperation(value = "根据ID获取表单数据信息")
    @RequestMapping(value =  "/api/FormData/{id}",method = {RequestMethod.GET})
    public ApiResult<FormData> Get(@PathVariable Long id){
        ApiResult<FormData> result=new ApiResult<FormData>();
        try
        {
            FormData model= dao.getOne(id);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 修改表单数据信息
     * @param id 表单数据ID
     * @param model 表单数据信息
     * @return api结果
     */
    @ApiOperation(value = "修改表单数据信息")
    @RequestMapping(value =  "/api/FormData/{id}",method = {RequestMethod.PUT})
    public ApiResult<FormData> Edit(@PathVariable Long id,@RequestBody FormData model){
        ApiResult<FormData> result=new ApiResult<FormData>();
        try
        {
            model.setFormDataId(id);
            dao.save(model);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 添加表单数据信息
     * @param model 表单数据信息
     * @return api结果
     */
    @ApiOperation(value = "添加表单数据信息")
    @RequestMapping(value =  "/api/FormData/",method = {RequestMethod.POST})
    public ApiResult<FormData> Add(@RequestBody FormData model){
         ApiResult<FormData> result=new ApiResult<FormData>();
        try
        {
            model.setCreateDate(new Date());
            dao.save(model);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除表单数据信息
     * @param id 表单数据ID
     * @return api结果
     */
    @ApiOperation(value = "删除表单数据信息")
    @RequestMapping(value =  "/api/FormData/{id}",method = {RequestMethod.DELETE})
    public ApiResult Delete(@PathVariable  Long id){
        ApiResult result=new ApiResult();
        try 
        {
            dao.deleteById(id);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}

